﻿// ----------------------------------------------------------------------------------
// Sample Code from Fernando Machado Píriz's blog: http://fernandomachadopiriz.com
//
// <copyright company="Fernando Machado Píriz" file="RunningProgram.cs">
// Copyright (c) Fernando Machado Píriz. Distributed under FSF's GPL v2.</copyright>
// ----------------------------------------------------------------------------------
// This code and information are provided "as is" without warranty of any kind,
// either expressed of implied, including but not limited to the implied warranties
// of merchantability and/or fitness for a particular purpose.
// ----------------------------------------------------------------------------------
namespace ClosePrograms
{
    using System;
    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis;
    using System.Security.Permissions;
    using GivingAPresentation.Commons;

    /// <summary>
    /// Contains information about a running program.
    /// </summary>
    public sealed class RunningProgram
    {
        /// <summary>
        /// The value of FriendlyName property.
        /// </summary>
        private string friendlyName;

        /// <summary>
        /// The value of Process property.
        /// </summary>
        private Process process;

        /// <summary>
        /// The value of ModuleFileName property.
        /// </summary>
        private string moduleFileName;

        /// <summary>
        /// Initializes a new instance of the RunningProgram class given the program's running process.
        /// </summary>
        /// <param name="process">The program's running process.</param>
        [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters",
            MessageId = "GivingAPresentation.Commons.ExceptionHelper.Check(System.Boolean,System.String)",
            Justification = "Resources will be used in a future version")]
        [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0",
            Justification = "Argument checked using ExceptionHelper.CheckArgumentNotNull")]
        [EnvironmentPermission(SecurityAction.Demand, Unrestricted = true)]
        public RunningProgram(Process process)
        {
            ExceptionHelper.CheckArgumentNotNull(process, "process");
            ExceptionHelper.Check(process.MainModule != null, "Given process module is null");
            ExceptionHelper.Check(!String.IsNullOrEmpty(process.MainModule.FileName), "Given process module file name is null or empty");

            Process = process;
        }

        /// <summary>
        /// Gets program's name. Automatically assigned when <code>Process</code> is assigned.
        /// </summary>
        public string FriendlyName
        {
            get
            {
                return this.friendlyName;
            }
        }

        /// <summary>
        /// Gets or sets the program's running process.
        /// </summary>
        [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters",
            MessageId = "GivingAPresentation.Commons.ExceptionHelper.Check(System.Boolean,System.String)",
            Justification = "Globalization will be added in a future version.")]
        [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0",
            Justification = "Argument checked using ExceptionHelper.CheckArgumentNotNull")]
        public Process Process
        {
            get
            {
                return this.process;
            }

            [EnvironmentPermission(SecurityAction.Demand, Unrestricted = true)]
            set
            {
                ExceptionHelper.CheckArgumentNotNull(value, "value");
                ExceptionHelper.Check(value.MainModule != null, "Assigned process module is null");
                ExceptionHelper.Check(!String.IsNullOrEmpty(value.MainModule.FileName), "Assigned process module file name is null or empty");

                this.process = value;
                this.moduleFileName = this.process.MainModule.FileName;
                this.friendlyName = ProgramsEnumerator.GetFriendlyName(this.moduleFileName);
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the program should be processed or not by <code>ProgramsEnumerator</code> methods processing
        /// lists of programs.
        /// </summary>
        public bool Selected { get; set; }

        /// <summary>
        /// Gets the full path to program's executable file. Automatically assigned when <code>Process</code> is assigned.
        /// </summary>
        public string ModuleFileName
        {
            get
            {
                return this.moduleFileName;
            }
        }
    }
}
